/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package controllers.changePassword;

import controllers.utils.SendMail;
import java.io.Serializable;
import javax.annotation.Resource;
import javax.inject.Named;
import javax.enterprise.context.RequestScoped;
import java.sql.*;
import javax.faces.bean.ViewScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import javax.sql.*;
import models.User;

/**
 *
 * @author Ida
 */
@Named("changePassword")
@RequestScoped
@ViewScoped
public class ChangePassword implements Serializable{

    private String newPassword;
    private String repeatNewPassword;
    private String oldPassword;
    @Resource(name="jdbc/sysprosjresource")
    private DataSource dataSource;
    private Connection conn = null;
    private PreparedStatement stmt = null;
    private ResultSet res = null;

    public ChangePassword() {
    }

    public String getNewPassword() {
        return newPassword;
    }

    public void setNewPassword(String newPassword) {
        this.newPassword = newPassword;
    }

    public String getRepeatNewPassword() {
        return repeatNewPassword;
    }

    public void setRepeatNewPassword(String repeatNewPassword) {
        this.repeatNewPassword = repeatNewPassword;
    }

    public String changepassword() {
        if (!newPassword.equals(repeatNewPassword)) {
            throw new RuntimeException("Should never reach here");
        }
        ExternalContext context = FacesContext.getCurrentInstance().getExternalContext();
        HttpServletRequest request = (HttpServletRequest) context.getRequest();
        try {
            conn = dataSource.getConnection();
            stmt = conn.prepareStatement("update users set password=? WHERE userName=?");
            stmt.setString(1, newPassword);
            stmt.setString(2, request.getRemoteUser());
            stmt.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException("Should never happend");
        } finally {
            controllers.utils.DatabaseHjelper.lukk(res);
            controllers.utils.DatabaseHjelper.lukk(stmt);
            controllers.utils.DatabaseHjelper.lukk(conn);
        }

        return "/faces/protected/passwordChanged.xhtml";
    }

        public String getOldPassword() {
        return oldPassword;
    }

    public void setOldPassword(String oldPassword) {
        this.oldPassword = oldPassword;
    }

    private void sendMail(User u){
        String content = "Hei " + u.getFirstName() + " " + u.getLastName()
                +". Din brukerinformasjon hos Emner'R'Us er oppdatert."
                + "\n\nBrukernavn: " + u.getUserName() + "\nPassord: " + u.getPassword()
                + "\n\n Mvh GRPS-10\nSystemeringsprosjekt 2011";
        String subject = "Emner'R'Us brukerprofil oppdatert.";
        SendMail sM = new SendMail(u.getEmail(), content, subject);
        sM.run();
    }
}
